define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	$scope.form.father_id=params.father_id;
	$scope.form.fk_unit='self';
	$scope.form.code = '01';
	$scope.dataInfo = {};
	$scope.isClassMajorShow = app.parameter.parameter_isClassMajorShow;

	//查询主校区和分校区
	httpService.post(app.urls.unit_selectSchoolAndAttach,{"fk_unit":$scope.form.fk_unit},function(data){
		if("0000" === data.code){
			$scope.unitList = data.data;
		}else{
			alert(data.msg);
		}
	});

	//查询年级
	$scope.findGrade = function() {
		if($scope.dataInfo.fk_unit == null) {
			$scope.form.fk_unit = "self";
		}else{
			$scope.form.fk_unit = $scope.dataInfo.fk_unit;
		}
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
				if(params.fk_grade!='undefined'){
					$scope.form.fk_grade=params.fk_grade;
					$scope.findmajor();
				}
				$scope.$apply();
				$scope.getName();
				$scope.findclassroom();
			}else{
				alert(data.msg);
			}
		});
	}

	//查询专业
	$scope.findmajor = function(){
		httpService.post(app.urls.major_select,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.majorList = data.data;
				$scope.getName();
			}else{
				alert(data.msg);
			}
		});
	}
	//查询教室
	$scope.findclassroom = function() {
		httpService.post(app.urls.classroom_select, {"fk_unit":$scope.form.fk_unit}, function (data) {
			if ("0000" === data.code) {
				$scope.classroomList = data.data;
			} else {
				alert(data.msg);
			}
		});
	}



	$('input[name="classNo"]').bind('input propertychange',function(){
		$scope.form.sort = $scope.form.sort.toString().replace(".","");
		$scope.getName();
		$scope.$apply();
	});
	$('input[name="major"]').bind('input propertychange',function(){
		$scope.getName();
		$scope.$apply();
	});
	$scope.form.sort = '1';

	$scope.getName = function() {
		$scope.form.gradename = $('#grade').find("option:selected").text();
		$scope.form.majorname = $('#major').find("option:selected").text();
		if($scope.form.fk_grade != undefined &&$scope.form.fk_grade !=null && $scope.form.fk_grade != ''){
			$scope.form.sort = $scope.form.sort.replace(/^(0+)|[^\d]+/g,'');
			if($scope.isClassMajorShow == "1"){
				if($scope.form.fk_major != undefined &&$scope.form.fk_major !=null && $scope.form.fk_major != ''){
					$scope.form.major = $scope.form.majorname;
					$('#major_name')[0].innerHTML = $scope.form.majorname;
				}else{
					$('#major_name')[0].innerHTML = "";
				}
			}
			$scope.form.show = true;
		}else{
			$scope.form.show = false;
		}
	}
	
	$scope.form.status = "0";
	$scope.saveForm = function(){
		if($scope.formCheck()){

			if($scope.isClassMajorShow == "1"){
				$scope.form.major = $('#major_name')[0].innerHTML;
				$scope.form.name = $scope.form.gradename + $scope.form.major +"("+$scope.form.sort+")班";
			}else{
				$scope.form.name = $scope.form.gradename + "("+$scope.form.sort+")班";
			}

			httpService.post(app.urls.class_selectWhenAdd,$scope.form,function(data){
				if("0000" === data.code){
					if(data.data.length == 0){
						httpService.post(app.urls.class_insert,$scope.form,function(data){
							if("0000" === data.code){
								eventBusService.publish("content.page.reload", data);
								eventBusService.publish("modal.page.close", {});
							}else{
				    			alert(data.msg);
				    		}
						});

					}else{
						alert("该班级已存在");
					}
	    		}else{
	    			alert(data.msg);
	    		}
			});
		}
	}
}}});
